package com.isias.system.Controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.isias.common.result.Result;
import com.isias.model.system.SysPost;
import com.isias.model.vo.SysPostQueryVo;
import com.isias.system.service.SysPostService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/admin/system/sysPost")
public class SysPostController {

    @Autowired
    private SysPostService sysPostService;

    /**
     * 分页查询
     */
    @GetMapping("{page}/{limit}")
    public Result SelectPage(@PathVariable Integer page, @PathVariable Integer limit, SysPostQueryVo sysPostQueryVo){
        Page<SysPost> sysPostPage = new Page<>();
        sysPostPage.setCurrent(page);
        sysPostPage.setSize(limit);
        IPage<SysPost> iPage = sysPostService.selectPage(sysPostPage,sysPostQueryVo);
        return Result.ok(iPage);

    }

    /**
     * 删除
     * @param id
     * @return
     */
    @DeleteMapping("{id}")
    public Result DeleteOne(@PathVariable String id){
        if (sysPostService.removeById(id)) {
            return Result.ok();
        }
        return Result.fail();

    }

    /**
     * 添加
     * @param sysPost
     * @return
     */
    @PostMapping
    public Result SaveOne(@RequestBody SysPost sysPost){
        if (sysPostService.save(sysPost)) {
            return Result.ok();
        }
        return Result.fail();
    }

    /**
     * 查询全部
     * @return
     */
    @GetMapping()
    public Result GetAll(){
        List<SysPost> all = sysPostService.GetAll();
        return Result.ok(all);
    }
    /**
     * 根据用户获取身份数据
     */
    @GetMapping("/toAssign/{userId}")
    public Result toAssign(@PathVariable String userId) {
        Map<String, Object> roleMap = sysPostService.getRolesByUserId(userId);
        return Result.ok(roleMap);
    }
    /**
     * 修改
     * @param sysPost
     * @return
     */
    @PutMapping
    public Result Updata(@RequestBody SysPost sysPost){
        if (sysPostService.updateById(sysPost)) {
            return Result.ok();
        }
        return Result.fail();
    }

    /**
     * 获取单个（数据回显）
     * @param id
     * @return
     */
    @GetMapping("{id}")
    public Result GetOne(@PathVariable String id){
        SysPost sysPost = sysPostService.getById(id);
        return Result.ok(sysPost);

    }

    /**
     * 更新岗位状态
     * @param id
     * @param status
     * @return
     */
    @GetMapping("/updataStatus/{id}/{status}")
    public Result ChangeStatus(@PathVariable String id,@PathVariable Integer status){
        if (sysPostService.changeStatus(id,status)){
            return Result.ok();
        }
        return Result.fail();
    }

    @DeleteMapping("/batchRemove")
    public Result DeleteByIDs(@RequestBody List<String> ids){
        if (sysPostService.removeByIds(ids)) {
            return Result.ok();
        }
        return Result.fail();
    }


}
